package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> list() {
        return customerMapper.selectSomeInfo();
    }

    @Override
    public Customer selectByPrimaryKey(Long custId) {
        Customer customer = customerMapper.selectByPrimaryKey(custId);
        return customer;
    }

    @Override
    public void updateByName(Long customerId, String customerName) {

        customerMapper.updateByName(customerId,customerName);
    }
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }


    public PageResult query(CustomerQueryObject qo) {
        int totalCount = customerMapper.queryForCount(qo);
        if(totalCount == 0){
            return new PageResult();
        }
        List<Customer> list = customerMapper.queryForList(qo);
        return new PageResult(totalCount,list);
    }

    public void insert(Customer customer) {

        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        Date inputDate = new Date();
        customer.setChargeUser(employee);
        customer.setInputUser(employee);
        customer.setInputDate(inputDate);
        customer.setStatus(1);
        customerMapper.insert(customer);

    }

    public void updateByPrimaryKey(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    public void lostCustomerById(Long id) {
        customerMapper.updateStatusByCustomerId(id,Customer.STATE_LOST);
    }

    public void shareCustomerById(Long id) {
        customerMapper.transToPoolByCustomerId(id,Customer.STATE_POOL);
    }

    public void absorbCustomerById(Long id) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        customerMapper.absorbCustomer(id,employee.getId(),Customer.STATE_POTENTIAL);
    }

    public void formalCustomerById(Long id) {
        customerMapper.updateStatusByCustomerId(id,Customer.STATE_VIP);
    }


}
